package homework.homework2;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class BooksMap {
    private Map<Integer, Book> booksMap = new HashMap();
    private Set<Integer> integers = booksMap.keySet();
    public Map<Integer, Book> car = new HashMap();
    private Integer num = 0;

    public BooksMap(){
        init();
    }

    private void init(){
        Book b1 = new Book(++num, "html+css", 10, 100);
        Book b2 = new Book(++num, "javascript", 100, 1000);
        Book b3 = new Book(++num, "javaoo编程", 199, 2000);
        Book b4 = new Book(++num, "数据库编程", 150, 1500);
        Book b5 = new Book(++num, "java基础", 20, 200);

        booksMap.put(b1.getNum(), b1);
        booksMap.put(b2.getNum(), b2);
        booksMap.put(b3.getNum(), b3);
        booksMap.put(b4.getNum(), b4);
        booksMap.put(b5.getNum(), b5);
    }

    public void run(){
        while(true){
            Scanner scan = new Scanner(System.in);
            System.out.println("欢迎来到 xxxx 书店:");
            System.out.println("=====================================================");
            System.out.println(show());

            System.out.println("请选择");
            System.out.println("1.购买  2.结账  3. 退出");
            System.out.println("=====================================================");

            String input = scan.next();
            if("1".equals(input)){
                byInCar();
            }else if("2".equals(input)){
                payInCar();
            }else if("3".equals(input)){
                System.exit(0);
            }else{
                System.out.println("输入不正确");
            }
        }
    }

    public void payInCar(){
        Set<Integer> integers = car.keySet();
        double money = 0;

        for (Integer integer : integers) {
            money = money + car.get(integer).getPrice() * car.get(integer).getMuch();
        }
        System.out.println("显示购物车的详细信息：");
        System.out.println(showCar());
        System.out.println("总价格：" + money + "元");
        System.out.println("1. 付款   2.继续购买");

        Scanner scan = new Scanner(System.in);
        String input = scan.next();

        if("1".equals(input)){
            System.out.println("支付" + money + "元");
            buyBook();
        }
    }

    public void byInCar(){

        Scanner scan = new Scanner(System.in);
        System.out.println("请输入图书编号：");
        int num = scan.nextInt();

        if(havaNum(num)){
            System.out.println("请输入购买数量：");
            int nums = scan.nextInt();

            if(nums > booksMap.get(num).getMuch()){
                System.out.println("数量大于库存数量");
            }else{
                Book b = new Book();
                b.setName(booksMap.get(num).getName());
                b.setPrice(booksMap.get(num).getPrice());
                b.setNum(booksMap.get(num).getNum());
                b.setMuch(nums);
                car.put(num,b);
                System.out.println("加入购物车成功");
            }
        }else{
            System.out.println("没有该编号的书籍");
        }

    }

    private boolean havaNum(int num){
        boolean flag = false;

        for (Integer integer : integers) {
            if(integer == num){
                flag = true;
                break;
            }
        }

        return flag;
    }

    public String show(){
        String s = "编号\t书名\t价格\t当前库存\n";

        for (Integer integer : this.integers) {
            s = s + booksMap.get(integer).getNum() + "\t" + booksMap.get(integer).getName() + "\t"
                    + booksMap.get(integer).getPrice() + "\t" + booksMap.get(integer).getMuch() + "\n";
        }

        return s;
    }

    public String showCar(){
        Set<Integer> integers = car.keySet();
        String s = "编号\t书名\t单价\t购买数量\t总和\n";

        for (Integer integer : integers) {
            s = s + car.get(integer).getNum() + "\t" + car.get(integer).getName() + "\t"
                    + car.get(integer).getPrice() + "\t" + car.get(integer).getMuch() + "\t"
                    + (car.get(integer).getPrice() * car.get(integer).getMuch()) + "\n";
        }

        return s;
    }

    public void buyBook(){
        Set<Integer> integers = car.keySet();

        for (Integer integer : integers) {
            booksMap.get(integer).setMuch(booksMap.get(integer).getMuch() - car.get(integer).getMuch());
        }

        car = new HashMap();
    }
}
